//
//  ReadTableViewController.m
//  MyReader
//
//  Created by hh on 15/12/11.
//  Copyright © 2015年 hh. All rights reserved.
//

#import "ReadViewController.h"
#import "ContentViewController.h"
#import "ViewController.h"
#import "AnimateViewController.h"

@interface ReadViewController () <UIPageViewControllerDataSource, UIPageViewControllerDelegate>
{
    UIPageViewController *pageViewCon;
//    UIToolbar *toolBar;
    
    CGFloat totalHeight;
    NSUInteger totalPages;
    NSUInteger curPage;
    NSUInteger lenPerPage;
    
    UIBarButtonItem *barItem;
}
@end

@implementation ReadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(onClick:)];
    
    pageViewCon = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:@{UIPageViewControllerOptionSpineLocationKey : @(UIPageViewControllerSpineLocationNone)}];
    pageViewCon.dataSource = self;
    pageViewCon.delegate = self;
    [self addChildViewController:pageViewCon];
    pageViewCon.view.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64 - 44);
//    pageViewCon.preferredContentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height - 64 - 44);
//    CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI/6);
//    transform = CGAffineTransformScale(transform, 0.8, 0.8);
//    pageViewCon.view.transform = transform;
    
    [self.view addSubview:pageViewCon.view];
    
//    toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 44, self.view.frame.size.width, 44)];
//    [self.view addSubview:toolBar];
    
    CGRect rect = [self.text boundingRectWithSize:CGSizeMake(self.view.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[[UILabel new].font fontWithSize:24.0]} context:nil];
    totalHeight = ceil(rect.size.height);
    totalPages = ceil(totalHeight / (self.view.frame.size.height - 64 - 44));
    lenPerPage = _text.length / totalPages;
    curPage = 0;
    
    barItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:@"第%u页／共%u页",curPage + 1, totalPages] style:UIBarButtonItemStylePlain target:nil action:nil];
//    toolBar.items = @[barItem];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"翻到最后一页" style:UIBarButtonItemStylePlain target:self action:@selector(gotoLast:)];
    self.toolbarItems = @[barItem, item];
    
    ContentViewController *con = [ContentViewController new];
    con.text = [_text substringWithRange:NSMakeRange(curPage * lenPerPage, lenPerPage)];
    [pageViewCon setViewControllers:@[con] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.toolbarHidden = NO;
}

- (void)onClick:(id)sender
{
    AnimateViewController *con = [AnimateViewController new];
    con.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:con animated:YES];
}

- (void)gotoLast:(id)sender
{
    curPage = totalPages - 1;
    ContentViewController *con = [self getCurContentController];
    [pageViewCon setViewControllers:@[con] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    [self refreshBarItemShow];
}

- (void)refreshBarItemShow
{
    barItem.title = [NSString stringWithFormat:@"第%u页／共%u页",curPage + 1, totalPages];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    if (curPage <= 0) {
        return nil;
    }
    curPage--;
    [self refreshBarItemShow];
    return [self getCurContentController];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    if (curPage >= totalPages - 1) {
        return nil;
    }
    curPage++;
    [self refreshBarItemShow];
    return [self getCurContentController];
}

- (ContentViewController *)getCurContentController
{
    ContentViewController *con = [ContentViewController new];
    con.text = [_text substringWithRange:NSMakeRange(curPage * lenPerPage, lenPerPage)];
    con.index = curPage;
    return con;
}

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed
{
//    NSLog(@"animate finished? %d, trans completed? %d", finished, completed);
    if (!completed) {
        ContentViewController *con = (ContentViewController *)previousViewControllers[0];
        curPage = con.index;
        [self refreshBarItemShow];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
